package com.itouch.bracelet.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.adapter.NearbyPeopleAdapter;
import com.itouch.bracelet.adapter.NearbyPeopleAdapter.AddLinstener;
import com.itouch.bracelet.bean.NearbyPeople;
import com.itouch.bracelet.bean.NearbyPeopleResult;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.bracelet.diseaseinfo.Constants;

/**
 * cocoaSJ
 */
public class NearByActivity extends BaseActivity implements 
		OnItemClickListener ,AddLinstener{

	private static final String LOG_TAG = "NearByActivity";
	private ListView listview;
	private ArrayList<NearbyPeople> list;
	private NearbyPeopleAdapter adapter;
	
	@Override
	protected int setContentViewById() {
		return R.layout.activity_nearby;
	}

	@Override
	protected void initViews() {
		listview = (ListView) findViewById(
				R.id.listview);
		list = new ArrayList<NearbyPeople>();
		adapter = new NearbyPeopleAdapter(this, list);
		adapter.setListener(this);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(this);
		getNearby("11","11");
	}

	@Override
	protected void initData() {
	}
	
	
	
	private void getNearby(String filter_S_lat,String filter_S_lng) {
		show();
		list.clear();
		
		
//			http://192.168.3.222:83/handchain/mobi/customer/calDistanceByPage?filter_S_lat=11&filter_S_lng=11&filter_S_id=85
		Uri.Builder builder = Uri.parse(Constants.GET_NEARBY)
				.buildUpon();
		builder.appendQueryParameter("filter_S_lat", filter_S_lat);
		builder.appendQueryParameter("filter_S_lng", filter_S_lng);
		builder.appendQueryParameter("filter_S_id", UserCache.getOauth(context).getUserId());
		Log.d("GET_NEARBY",
				"GET_NEARBY==POST" + builder.toString());
		getJSONByVolleyGET(builder.toString(), 
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();
						Log.d("GET_NEARBY",
								"GET_NEARBY" + arg0.toString());
						try {
							NearbyPeopleResult a = new Gson().fromJson(
									arg0.toString(), NearbyPeopleResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
								
								List<NearbyPeople> apiList = a.getRows();
								if(apiList!=null&apiList.size()>0){
									list.addAll(apiList);
									adapter.notifyDataSetChanged();
								}
								
								
								
								
								
							} else {
								Toast.makeText(context,
										a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	public void add(int position) {
		try{
			NearbyPeople a = list.get(position);
			if("0".equals(a.getNum())){
				//添加好友	
				
				addFriend(a.getId());
			}
		}catch(Exception x){
			
		}
		
	}
	
	
	private void addFriend(String receiver) {
		show();
		Uri.Builder builder = Uri.parse(Constants.ADD_FRIEND)
				.buildUpon();
		builder.appendQueryParameter("receiver", receiver);
		builder.appendQueryParameter("initiator", UserCache.getOauth(context).getUserId());
		Log.d("ADD_FRIEND",
				"ADD_FRIEND==POST" + builder.toString());
		getJSONByVolleyGET(builder.toString(), 
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();
						Log.d("ADD_FRIEND",
								"ADD_FRIEND" + arg0.toString());
						try {
							NearbyPeopleResult a = new Gson().fromJson(
									arg0.toString(), NearbyPeopleResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
								getNearby("11","11");
							
							} else {
								Toast.makeText(context,
										a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	
	
}